//
//  allocArray.cpp
//  learnC++
//
//  Created by apple on 2018/5/29.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#include "allocArray.hpp"
#include <string>

void test_alloc_array(){

    //返回指向该内存块的指针
    int *ptr = new int[5]();//初始化
    
    ptr[0] = 5;
    
    for (int i = 0; i < 5; ++i) {
        printf("%d\n",ptr[i]);
    }
    
    //    “数组删除怎么知道要删除多少内存？”答案是数组new []跟踪有多少内存被分配给一个变量，因此数组delete []可以删除适量。不幸的是，这个大小/长度是程序员无法访问的。
    delete [] ptr;
    
    //统一初始化
    
    int *array = new int[5] { 9, 7, 5, 3, 1 };
//    释放new分配的对象数组指针指向的内存
    delete [] array;
    
    int fixedArray[5] { 9, 7, 5, 3, 1 };
    char fixedArray2[14] { "Hello, world!" };
    
    char *arr = new char[14]{"hello"};
    
    delete [] arr;
    
    //new该种方式无法获取数组大小，最好用系统封装的数组
    std::string *ss = new std::string[3]{"str1","str2","str3"};
    ss[0] = "hehe";
    
    delete [] ss;
    
    std::cout << "array" << *fixedArray << fixedArray2 << arr << "\n";
    
}
